<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录</title>
  <script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <!-- 引入 axios -->
  <script src="config.js" type="module"></script>

  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #f0f2f5;
    }
    .login-container {
      background: white;
      padding: 40px;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      width: 350px;
    }
    .login-title {
      font-size: 24px;
      margin-bottom: 20px;
      text-align: center;
    }
    .form-group {
      margin-bottom: 20px;
    }
    .form-group label {
      display: block;
      margin-bottom: 8px;
    }
    .form-group input {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 16px;
    }
    .login-btn {
      width: 100%;
      padding: 12px;
      background-color: #1890ff;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
    }
    .login-btn:hover {
      opacity: 0.9;
    }
    .error-message {
      color: #f5222d;
      margin-top: 10px;
      text-align: center;
    }
    .register-link {
      margin-top: 20px;
      text-align: center;
    }
    .register-link a {
      color: #1890ff;
      text-decoration: none;
    }

  </style>
</head>
<body>
  <div id="login-page" class="login-container">
    <div class="login-title">用户登录</div>
    <div class="form-group">
      <label>用户名</label>
      <input type="text" v-model="username" placeholder="请输入用户名">
    </div>
    <div class="form-group">
      <label>密码</label>
      <input type="password" v-model="password" placeholder="请输入密码">
    </div>
    <button class="login-btn" @click="login">登录</button>
    <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
  </div>

</body>
</html>
<script>

  new Vue({
    el: '#login-page',
    data() {
      return {
        username: '',
        password: '',
        errorMessage: ''
      }
    },
    methods: {
      async login() {
        if (!this.username || !this.password) {
          this.errorMessage = '请输入用户名和密码';
          return;
        }
          const response = await axiosInstance.post('/user/login', {
            username: this.username,
            password: this.password
          });
          console.log('响应结果:', response); // 打印响应结果

          if (response.data.code==200) {
            // 存储 userId
            localStorage.setItem('userId', response.data.data.userId);
            window.location.href = 'search.html'
          } else {
            this.errorMessage = response.message || '登录失败';
          }

      }
    }
  });
</script>